package com.mz.ozy.activity.me;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mz.ozy.R;
import com.mz.ozy.activity.assets.ExchangeActivity;
import com.mz.ozy.activity.home.ExchangeDetailActivity;
import com.mz.ozy.activity.home.SimpleShareListener;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AccountBean;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.MyBusBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MyBusPresenter;
import com.mz.ozy.mvp.view.MyBusContract;
import com.mz.ozy.utils.BitmapUtils;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.dialog.BaseDialog;
import com.mz.ozy.widget.dialog.MyCardEditDialog;
import com.mz.ozy.widget.dialog.ShareDialog;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import org.json.JSONObject;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/22 0022.
 */
public class BusinessCardActivity extends BaseActivity<MyBusContract.View, MyBusPresenter> implements MyBusContract.View {
    @BindView(R.id.tv_bind_wx)
    TextView tvBindWx;
    @BindView(R.id.tv_bind_qq)
    TextView tvBindQq;
    @BindView(R.id.tv_bind_company)
    TextView tvBindCompany;
    @BindView(R.id.tv_bind_zz)
    TextView tvBindZz;
    @BindView(R.id.tv_bind_add)
    TextView tvBindAdd;
    @BindView(R.id.userIconIv)
    ImageView userIconIv;
    @BindView(R.id.tv_grade)
    TextView ivMeRight;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.rele_all)
    RelativeLayout rele_all;

    private MyCardEditDialog mNameDialog;
    private Bitmap mBitmap;
    private String type1;
    private int shiming = 0;
    private AccountBean dataItem;
    private MeBean bean;
    private MyBusBean mBusBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_card);
        QMUIStatusBarHelper.translucent(this);
        initTitle();
        setTitleBarMargin();
        tvBack.setImageResource(R.mipmap.white_back);
        tvCenterTitle.setText("我的名片");
        tvCenterTitle.setTypeface(Typeface.DEFAULT_BOLD);
        tvCenterTitle.setTextColor(ContextCompat.getColor(this, R.color.white));
//        StatusBarUtil.setColor(BusinessCardActivity.this, getResources().getColor(R.color.c_73b21a), 100);
        ButterKnife.bind(this);
        shiming = getIntent().getIntExtra("shiming", 0);
        mBitmap = BitmapUtils.createBitmap(rele_all);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getInfo(this);
        mPresenter.getMeInfo(this);
        mPresenter.upAccountHeader(this);
    }

    @OnClick({R.id.back, R.id.tv_bind_wx, R.id.tv_bind_qq, R.id.tv_bind_company, R.id.tv_bind_zz, R.id.tv_bind_add, R.id.btn_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_bind_wx:
                showBindDialog(tvBindWx, "微信", "wechatid");
                break;
            case R.id.tv_bind_qq:
                showBindDialog(tvBindQq, "QQ", "qqno");
                break;
            case R.id.tv_bind_company:
                showBindDialog(tvBindCompany, "公司", "company");
                break;
            case R.id.tv_bind_zz:
                showBindDialog(tvBindZz, "职业", "position");
                break;
            case R.id.tv_bind_add:
                showBindDialog(tvBindAdd, "地址", "address");
                break;
            case R.id.btn_share:
                if (dataItem.getShiming() == 1) {
                    showShareDialog();
                } else {
                    final BaseDialog dialog = new BaseDialog(BusinessCardActivity.this, R.layout.dialog_private, Gravity.CENTER, true);
                    ((TextView) dialog.getView(R.id.tv_dialog_content)).setText("请先实名认证");
                    ((TextView) dialog.getView(R.id.tv_dialog_title)).setText("实名认证");
                    ((TextView) dialog.getView(R.id.btn_confirm)).setText("去实名");
                    dialog.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.toggleDialog();
                        }
                    });
                    dialog.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            VerifiedActivity.go(BusinessCardActivity.this, "", "", 0);
                            dialog.toggleDialog();
                        }
                    });
                    dialog.toggleDialog();
                }
                break;
        }
    }

    private ShareDialog mDialog;

    /**
     * 显示分享弹窗
     */
    private void showShareDialog() {
        if (mDialog == null) {
            mDialog = new ShareDialog(this);
            mDialog.setOnDialogListener(new ShareDialog.onDialogListener() {
                @Override
                public void onConfirm(SHARE_MEDIA platform) {
                    shareSingleImage(platform);
                    mDialog.toggleDialog();
                }
            });
        }
        mDialog.toggleDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    public void shareSingleImage(SHARE_MEDIA platform) {
        ShareAction shareAction = new ShareAction(this);
        shareAction.withMedia(getShareBitmap()).setPlatform(platform).setCallback(new SimpleShareListener() {

            @Override
            public void onResult(SHARE_MEDIA shareMedia) {
//                if (shareMedia == SHARE_MEDIA.SINA) {
//                ToastUtils.showToast("分享成功");
//                }
            }

            @Override
            public void onStart(SHARE_MEDIA shareMedia) {
                super.onStart(shareMedia);
                if ((shareMedia == SHARE_MEDIA.WEIXIN || shareMedia == SHARE_MEDIA.WEIXIN_CIRCLE) && !UMShareAPI.get(BusinessCardActivity.this)
                        .isInstall(BusinessCardActivity.this, SHARE_MEDIA.WEIXIN)) {
                    ToastUtils.showToast("您未安装微信客户端");
                }
                if (shareMedia == SHARE_MEDIA.QQ && !UMShareAPI.get(BusinessCardActivity.this).isInstall(BusinessCardActivity.this, SHARE_MEDIA.QQ)) {
                    ToastUtils.showToast("您未安装QQ客户端");
                }
                if (shareMedia == SHARE_MEDIA.SINA && !UMShareAPI.get(BusinessCardActivity.this).isInstall(BusinessCardActivity.this, SHARE_MEDIA.SINA)) {
                    ToastUtils.showToast("您未安装微博客户端");
                }
            }

        }).share();
    }

    private UMWeb getShareBitmap() {
        String yourName = "";
        if (!TextUtils.isEmpty(dataItem.getUname())) {
            yourName = dataItem.getUname();
        } else if (!TextUtils.isEmpty(dataItem.getUsername())) {
            yourName = dataItem.getUsername();
        }
        String userlink = Constants.KEY.SHARL + mBusBean.getInvitecode();
        UMWeb web = new UMWeb(userlink);//连接地址
        web.setTitle(yourName);//标题
        web.setDescription(yourName + "给您分享了他的名片，快来看看吧！");//描述
        if (TextUtils.isEmpty(dataItem.getUseravatar())) {
            web.setThumb(new UMImage(this, R.mipmap.logo)); //本地缩略图
        } else {
            web.setThumb(new UMImage(this, dataItem.getUseravatar())); //网络缩略图
        }
        return web;
    }


    private void showBindDialog(TextView textView, String type, String name1) {
        try {

            mNameDialog = new MyCardEditDialog(this, type);
            mNameDialog.setOnDialogListener(name -> {
                textView.setText(name);
                type1 = name;
                mPresenter.upload(this, name1, name);
            });

            mNameDialog.toggleDialog();
        } catch (Exception e) {

        }

    }

    //"https://napi.ozylives.com/abc/ozy/front/card.html?d=%@",_userInfoModel.invitecode
    @Override
    public void getInfoSuccess(MyBusBean bean) {
        mBusBean = bean;
        GlideUtils.loadAvatar(this, userIconIv, bean.getUseravatar());
        tvPhone.setText("专属ID:" + bean.getInvitecode());
        tvBindWx.setText(TextUtils.isEmpty(bean.getWechatid()) ? "未设置" : bean.getWechatid());
        tvBindQq.setText(TextUtils.isEmpty(bean.getQqno()) ? "未设置" : bean.getQqno());
        tvBindCompany.setText(TextUtils.isEmpty(bean.getCompany()) ? "未设置" : bean.getCompany());
        tvBindZz.setText(TextUtils.isEmpty(bean.getPosition()) ? "未设置" : bean.getPosition());
        tvBindAdd.setText(TextUtils.isEmpty(bean.getAddress()) ? "未设置" : bean.getAddress());
        switch (bean.getGrade()) {
            case 0:
                ivMeRight.setText("普通会员");
                break;
            case 1:
                ivMeRight.setText("VIP");
                break;
            case 2:
                ivMeRight.setText("金卡VIP");
                break;
            case 3:
                ivMeRight.setText("钻卡VIP");
                break;
            case 4:
                ivMeRight.setText("健康顾问");
                break;
            case 5:
                ivMeRight.setText("资深顾问");
                break;
            case 6:
                ivMeRight.setText("蓝宝顾问");
                break;
            case 7:
                ivMeRight.setText("绿宝顾问");
                break;
            case 8:
                ivMeRight.setText("红宝督导");
                break;
            case 9:
                ivMeRight.setText("蓝钻督导");
                break;
            case 10:
                ivMeRight.setText("绿钻督导");
                break;
            case 11:
                ivMeRight.setText("红钻督导");
                break;
            case 12:
                ivMeRight.setText("黑钻首席");
                break;
        }
    }

    @Override
    public void getAccountSuccess(AccountBean bean) {
        dataItem = bean;
    }

    @Override
    public void uploadSuccess(JSONObject ben) {
        ToastUtils.showToast(type1 + "修改成功");
    }

    @Override
    public void getgetMeInfoSuccess(MeBean bean) {
        this.bean = bean;
        if (!TextUtils.isEmpty(bean.getUname())) {
            tvUsername.setText(bean.getUname());
        } else {
            tvUsername.setText("未实名");
        }

    }
}
